#!/usr/bin/perl
#
#
%trans = ();
while($line=<STDIN>) {
	chomp($line);
	if($line=~/^------------------------/) {
		open(STR,">mStr.txt");
		foreach $t (@{$trans{$node}}) {
			print STR $t."\n";
		}
		close(STR);
		`ngram -ppl mStr.txt -lm /home1/sriram/Vaanee/subtreeLM/sentences.lm -debug 1 >mStr.scr.txt 2>mStr.scr.err`;
		$bestPPX = 1000;
		$bestSen = "";

		open(LMSCR,"mStr.scr.txt");
		while($x=<LMSCR>) {
			chomp($x);
			$numWords = scalar(split(/\s+/,$x));
			$y=<LMSCR>; $y=<LMSCR>;
			$y=~/ppl= ([0-9\.]+)/;

			$a = $1;
			$PPX = $a**(1/$numWords);
		        
			if($PPX<$bestPPX) {
				$bestPPX = $PPX;
				$bestSen = $x;
			}	
			$y = <LMSCR>;
		}
		close(LMSCR);
		print $bestSen."\n";

		%trans=();
	}
	($node,$translation) = split(/\t/,$line);
	push(@{$trans{$node}},$translation);
}
